home *** CD-ROM | disk | FTP | other *** search
Text File | 2000-09-28 | 16.0 KB | 574 lines | [TEXT/CWIE] |
- /*************************************************************************
- ** Apple Macintosh Developer Technical Support
- **
- ** PPP Control strip Module
- **
- ** by Vinne Moscaritolo, <vinnie@apple.com>
- ** Apple Developer Technical Support
- **
- ** Created from as much sample code I could find.
- ** " Good Artists Imitate, Great Artist Steal "
- **
- ** File: PPP Control.cp
- **
- ** Copyright © 1996 Apple Computer, Inc.
- ** All rights reserved.
- **
- ** You may incorporate this sample code into your applications without
- ** restriction, though the sample code has been provided "AS IS" and the
- ** responsibility for its operation is 100% yours. However, what you are
- ** not permitted to do is to redistribute the source as "DSC Sample Code"
- ** after having made changes. If you're going to re-distribute the source,
- ** we require that you make it clear in the source that the code was
- ** descended from Apple Sample Code, but that you've made changes.
- **
- **************************************************************************/
-
-
-
- //------------------------------------------------------------------------------------
- #pragma mark Includes
- //------------------------------------------------------------------------------------
-
- #include <Exception.h>
- #include <A4Stuff.h>
-
- #include <ControlStrip.h>
- #include <OpenTransport.h>
-
- #include "OpenTptPPP.h" // Hopefully this will be in the univeral someday
-
-
- //------------------------------------------------------------------------------------
- #pragma mark Defines
- //------------------------------------------------------------------------------------
-
- #define USES_ASYNC_OPENS 0
-
- #define kPPPCreator 'rmot'
- #define kModemCreator 'modm'
- #define kControlPanelType 'cdev'
-
- #define kBaseIconID 129
- #define kPopupArrowPictID 256
- #define kMenuID 256
-
- enum { kPPPLoaded = 0 ,
- kPPPNotLoaded,
- kPPPFailed,
- kPPPOpen };
-
- #define mOpenPPPCP 1
- #define mOpenModemCP 2
-
- #define mDisconnect 4
- #define mSomethig 5
-
- #define IconWidth 16
-
- #define MAX_ICONS 4
-
- #define ThrowIfNil( _val_ ) \
- if ( (_val_) == nil ) \
- throw
-
- #define ThrowIfNotNil( _val_ ) \
- if ( (_val_) != nil ) \
- throw
-
- #define ThrowIfOSErr(_err_) \
- if ((_err_) != noErr) \
- throw
-
- #define ThrowOSErr(_err_) \
- throw
-
- long GetCurrentA5(void):__D0 = 0x200D; /* move.l A5,D0 */
- long SetA5(long:__D0):__D0 = 0xC18D; /* EXG D0,A5 */
- void SyncA5fromA4(void) = 0x2A4C; /* move.l A4,A5 */
-
- //------------------------------------------------------------------------------------
- #pragma mark Prototypes
- //------------------------------------------------------------------------------------
- void OpenControlPanel (OSType folder, OSType fdCreator, OSType fdType);
-
-
- //------------------------------------------------------------------------------------
- #pragma mark -
-
-
- //------------------------------------------------------------------------------------
- #pragma mark class CSM
- //------------------------------------------------------------------------------------
-
- class CSM {
-
- public:
- CSM();
- ~CSM();
-
- long GetDisplayWidth ();
- long Periodic (Rect *, GrafPtr);
- long Draw (Rect *, GrafPtr);
- long MouseDown (Rect *, GrafPtr);
- long SaveSettings ();
- long ShowBalloonHelp ();
-
- static pascal void NotifyProc(CSM*, OTEventCode, OTResult, void*);
-
- private:
- Boolean UpdateStatus ();
-
- protected:
- Handle fIcons[MAX_ICONS];
- MenuHandle fMenu;
- PicHandle fPopupArrowPicture;
- EndpointRef fEndPoint;
- unsigned long fPPPState;
- short fCurrentIcon;
- short fState;
- };
-
- //------------------------------------------------------------------------------------
- CSM::CSM()
- //------------------------------------------------------------------------------------
- {
- short i;
- long response;
-
- // OpenTransport Must be present
- ThrowIfOSErr (::Gestalt(gestaltOpenTpt, &response));
- if (! (response & (1 << gestaltOpenTptPresentBit))) throw;
-
- // OpenTransport/ Remote Access must also be present
- ThrowIfOSErr (::Gestalt(gestaltOpenTptRemoteAccess, &response));
- if (!(response & (1 << gestaltOpenTptRemoteAccessPresent))) throw;
-
- // OpenTransport/ Remote Access PPP must also be present
- if (! (response & (1 << gestaltOpenTptPPPPresent))) throw;
-
- // load and detach the icon suites
- for(i = 0; i < MAX_ICONS; i++)
- ThrowIfOSErr( ::SBGetDetachIconSuite(&fIcons[i], kBaseIconID+i, svAllSmallData));
-
- // load and detach the configuration menu
- ThrowIfNil (fMenu = ::GetMenu(kMenuID) );
- ::DetachResource((Handle)fMenu);
-
- // load and detach the ‘up arrow’ picture
- ThrowIfNil (fPopupArrowPicture = ::GetPicture(kPopupArrowPictID) );
- ::DetachResource((Handle)fPopupArrowPicture);
-
- fState = kPPPNotLoaded;
- fPPPState = -1;
- fEndPoint = kOTInvalidEndpointRef;
-
- {
- long old_A5 = GetCurrentA5();
- SyncA5fromA4();
-
- // Initialize OT
- if (InitOpenTransport()!= kOTNoError) fState = kPPPFailed;
-
- (void) SetA5(old_A5);
- }
-
- }
-
-
- //------------------------------------------------------------------------------------
- CSM::~CSM()
- //------------------------------------------------------------------------------------
- {
- // don't forget to write this
- };
-
- //------------------------------------------------------------------------------------
- long CSM::GetDisplayWidth()
- //------------------------------------------------------------------------------------
- {
- return (IconWidth +
- (**fPopupArrowPicture).picFrame.right - (**fPopupArrowPicture).picFrame.left);
- }
-
- //------------------------------------------------------------------------------------
- long CSM::Periodic(Rect *theRect, GrafPtr thePort)
- //------------------------------------------------------------------------------------
- {
- if( !UpdateStatus() ) return 0;
-
- theRect->right = theRect->left + IconWidth;
- ::EraseRect(theRect);
- Draw(theRect,thePort);
-
- return 1<<sdevHelpStateChange;
- }
-
- //------------------------------------------------------------------------------------
- long CSM::Draw(Rect *theRect, GrafPtr thePort)
- //------------------------------------------------------------------------------------
- {
- short arrowHeight;
-
- // draw the current icon
- theRect->right = theRect->left + IconWidth;
- (void) ::PlotIconSuite(theRect, atNone, ttNone, fIcons[fState] );
-
- // draw an ‘up arrow’ to show that the module has a popup menu
- arrowHeight = (**fPopupArrowPicture).picFrame.bottom - (**fPopupArrowPicture).picFrame.top;
- theRect->left = theRect->right;
- theRect->right += (**fPopupArrowPicture).picFrame.right - (**fPopupArrowPicture).picFrame.left;
- theRect->top = (theRect->top + theRect->bottom - arrowHeight) >> 1;
- theRect->bottom = theRect->top + arrowHeight;
- ::DrawPicture(fPopupArrowPicture, theRect);
-
- return 0;
- }
-
- //------------------------------------------------------------------------------------
- long CSM::MouseDown(Rect *theRect, GrafPtr thePort)
- //------------------------------------------------------------------------------------
- {
- short menuItem;
- long result;
-
- menuItem = SBTrackPopupMenu(theRect, fMenu);
- result = 0;
-
- switch (menuItem){
- case mOpenPPPCP:
- OpenControlPanel(kControlPanelFolderType,kPPPCreator, kControlPanelType);
- break;
-
- case mOpenModemCP:
- OpenControlPanel(kControlPanelFolderType,kModemCreator, kControlPanelType);
- break;
-
- case mDisconnect:
- DebugStr((ConstStr255Param)"/pPPP Disconnect");
- break;
- }
-
- return(result);
- }
-
- //------------------------------------------------------------------------------------
- long CSM::SaveSettings()
- //------------------------------------------------------------------------------------
- {
- return 0;
- }
-
- //------------------------------------------------------------------------------------
- long CSM::ShowBalloonHelp()
- //------------------------------------------------------------------------------------
- {
- return 0;
- }
-
-
- //------------------------------------------------------------------------------------
- Boolean CSM::UpdateStatus()
- //------------------------------------------------------------------------------------
- //
- // rwrite this uglyness with a real state machine...
- //
- {
-
- switch(fState)
- {
- case kPPPNotLoaded:
- {
-
- // first check to see if PPP got loaded yet.
- long response;
- ::Gestalt(gestaltOpenTptRemoteAccess, &response);
- if (!(response & (1 << gestaltOpenTptRemoteAccessLoaded))) return false;
-
- fState = kPPPLoaded;
-
- {
- long old_A5 = GetCurrentA5();
- SyncA5fromA4();
-
- // maybe there is a bug in the configuartor,
- // that wont allow openasyncs on the first endpoint.
-
- #if USES_ASYNC_OPENS
-
- // DebugStr("\p CSM::UpdateStatus - OTAsyncOpenEndpoint");
- if( ::OTAsyncOpenEndpoint( OTCreateConfiguration(kPPPControlName),0, nil,(OTNotifyProcPtr) NotifyProc, this )
- != kOTNoError) fState = kPPPFailed;
- #else
- OTResult result;
- // DebugStr("\p CSM::UpdateStatus - OTOpenEndpoint");
-
- fEndPoint = ::OTOpenEndpoint(OTCreateConfiguration(kPPPControlName),0, nil, &result);
- if( (result == kOTNoError) && (fEndPoint != kOTInvalidEndpointRef))
- if( (result = fEndPoint->InstallNotifier((OTNotifyProcPtr) NotifyProc, (void*) this)) == kOTNoError)
- {
- // fState = kPPPOpen;
- OTSetAsynchronous(fEndPoint);
- if( ::OTIoctl(fEndPoint, I_OTGetMiscellaneousEvents, (void*)1) != kOTNoError ) fState = kPPPFailed;
- }
- #endif
- (void) SetA5(old_A5);
- }
- }
- return true;
-
- case kPPPLoaded:
- case kPPPFailed:
- return false;
-
- case kPPPOpen:
-
- // PPP_OPT_GETCURRENTSTATE fPPPState
- // OTOptionManagement(fEndPoint,Cmd,Ret)
-
- return true;
- }
- return false;
-
- }
-
-
- //------------------------------------------------------------------------------------
- pascal void CSM::NotifyProc(CSM* csm, OTEventCode code, OTResult result, void* cookie)
- //------------------------------------------------------------------------------------
- {
- long old_A4 = SetCurrentA4();
- // long old_A5 = GetCurrentA5();
- // SyncA5fromA4();
-
- switch(code)
- {
- // Endpoint opened
- case T_OPENCOMPLETE:
- csm->fState = kPPPFailed;
-
- if(result != kOTNoError)
- DebugStr("\p CSM::NotifyProc - T_OPENCOMPLETE Failed; d3.w; ");
-
- //if(result == kOTAccessErr) DebugStr("\p CSM::NotifyProc - Missing access permission");
- if(result != kOTNoError) break;
-
- csm->fEndPoint = (EndpointRef) cookie;
-
- // ask PPP for misc events
- if( ::OTIoctl(csm->fEndPoint, I_OTGetMiscellaneousEvents, (void*)1) != kOTNoError ) break;
- csm->fState = kPPPOpen;
- break;
-
- case kStreamIoctlEvent:
- if(result != kOTNoError){
- DebugStr("\p PPP CSM::NotifyProc - kStreamIoctlEvent");
- csm->fState = kPPPFailed;
- break;
- };
- csm->fState = kPPPOpen;
-
- break;
-
- case T_OPTMGMTCOMPLETE:
- break;
-
- // PPP events
- case kPPPConnectCompleteEvent:
- case kPPPSetScriptCompleteEvent :
- case kPPPDisconnectCompleteEvent:
- case kPPPDisconnectEvent:
- case kPPPIPCPUpEvent:
- case kPPPIPCPDownEvent:
- case kPPPLCPUpEvent:
- case kPPPLCPDownEvent:
- case kPPPLowerLayerUpEvent:
- case kPPPLowerLayerDownEvent:
- case kPPPAuthenticationStartedEvent:
- case kPPPAuthenticationFinishedEvent:
- case kPPPDCEInitStartedEvent:
- case kPPPDCEInitFinishedEvent:
- case kPPPDCECallStartedEvent:
- case kPPPDCECallFinishedEvent:
-
- long i = code - kPPPEvent;
- //error code comes in cookie for PPP misc events
- if( ((OTResult)cookie) != kOTNoError) DebugStr("\p PPP CSM::NotifyProc - PPP event failed.; d3.w; d0.w;");
- // else DebugStr("\p PPP CSM::NotifyProc - PPP event; d3.w;");
- default:;
- }
-
- // (void) SetA5(old_A5);
- (void) SetA4(old_A4);
- }
-
-
- #pragma mark -
-
- //------------------------------------------------------------------------------------
- void OpenControlPanel (OSType folder, OSType fdCreator, OSType fdType)
- //------------------------------------------------------------------------------------
- {
- #define kFinderSig 'FNDR'
- #define kSystemType 'MACS'
- #define kAEOpenSelection 'sope'
- #define aeSelectionKeyword 'fsel'
-
- ProcessInfoRec processInfo;
- ProcessSerialNumber frontProcess;
- Boolean sameProcess;
- short vRefNum;
- long dirID;
- HFileInfo pb;
- Str63 filename;
- FSSpec theSpec;
- AliasHandle fileAlias, folderAlias;
- OSType finderSig;
- AEAddressDesc target;
- AppleEvent theEvent, ignoreReply;
- AEDescList fileList;
-
-
- // Find the Appropriate folder
- if (FindFolder(kOnSystemDisk, folder, kDontCreateFolder, &vRefNum, &dirID)) return;
-
- // Scan folder looking for file
- pb.ioNamePtr = filename;
- pb.ioVRefNum = vRefNum;
- pb.ioFVersNum = 0;
- pb.ioFDirIndex = 1;
- pb.ioDirID = dirID;
- while (! PBHGetFInfoSync((HParmBlkPtr)&pb)) {
- // if ((pb.ioFlFndrInfo.fdType == fdType) && (pb.ioFlFndrInfo.fdCreator == fdCreator))
- if (pb.ioFlFndrInfo.fdCreator == fdCreator)
- goto foundFile;
-
- pb.ioFDirIndex++;
- pb.ioDirID = dirID;
- }
- return;
-
- foundFile:
-
- // make FSS for file
- if (FSMakeFSSpec(vRefNum, dirID, filename, &theSpec)) return;
-
- // Create alias for file
- if (NewAliasMinimal(&theSpec, &fileAlias)) return;
-
- // try to find the Finder's process and bring it to the front
- processInfo.processInfoLength = sizeof(ProcessInfoRec); // initialize the process info record
- processInfo.processName = nil;
- processInfo.processNumber.highLongOfPSN = 0;
- processInfo.processNumber.lowLongOfPSN = kNoProcess;
- processInfo.processType = 0;
- processInfo.processAppSpec = nil;
-
- while (processInfo.processType != kFinderSig) { // run thru each of the current processes...
- if (GetNextProcess(&processInfo.processNumber) ||
- GetProcessInformation(&processInfo.processNumber, &processInfo)) return;
-
- if (processInfo.processType == kFinderSig) { // if this process belongs to the Finder,
-
- if (GetFrontProcess(&frontProcess) || // find out if it's in front
- SameProcess(&processInfo.processNumber, &frontProcess, &sameProcess)) return;
-
- if (! sameProcess && // if it isn't, try to bring it to the front
- SetFrontProcess(&processInfo.processNumber)) return;
- }
- }
-
-
- // send the Finder an AppleEvent to open the file
- finderSig = kSystemType;
- (void) AECreateDesc(typeApplSignature, (Ptr) &finderSig, sizeof(finderSig), &target);
- (void) AECreateAppleEvent(kFinderSig, kAEOpenSelection, &target, kAutoGenerateReturnID, kAnyTransactionID, &theEvent);
- (void) AEDisposeDesc(&target);
-
- (void) FSMakeFSSpec(vRefNum, dirID, nil, &theSpec);
- (void) NewAliasMinimal(&theSpec, &folderAlias);
- HLock((Handle)folderAlias);
- (void) AEPutParamPtr(&theEvent, keyDirectObject, typeAlias, (Ptr)*folderAlias, (**folderAlias).aliasSize);
- HUnlock((Handle)folderAlias);
-
- // Fill w/ file list
- (void) AECreateList(nil, 0, false, &fileList);
- HLock((Handle)fileAlias);
- (void) AEPutPtr(&fileList, 1, typeAlias, (Ptr)*fileAlias, (**fileAlias).aliasSize);
- HUnlock((Handle)fileAlias);
- (void) AEPutParamDesc(&theEvent, aeSelectionKeyword, &fileList);
- DisposHandle((Handle)fileAlias);
- (void) AEDeleteItem(&fileList, 1);
- (void) AEDisposeDesc(&fileList);
-
- (void) AESend(&theEvent, &ignoreReply, kAENoReply+kAENeverInteract, kAEHighPriority,
- kAEDefaultTimeout, nil, nil);
- (void) AEDisposeDesc(&theEvent);
-
- DisposHandle((Handle)folderAlias);
- };
-
-
- #pragma mark -
- //------------------------------------------------------------------------------------
- pascal long main(unsigned long message, CSM* csm, Rect *statusRect, GrafPtr statusPort)
- //------------------------------------------------------------------------------------
-
- {
- long old_A4 = SetCurrentA4();
- long result;
-
- result = 0;
-
- switch(message) {
- case sdevInitModule:
- try {
- result = (long) new CSM;
- }
- catch (...)
- {
- result = 0;
- }
- break;
-
- case sdevCloseModule:
- delete csm;
- break;
-
- case sdevFeatures:
- result = ( (1<<sdevWantMouseClicks) |\
- (1<<sdevDontAutoTrack) |\
- (1<<sdevHasCustomHelp) |\
- (1<<sdevKeepModuleLocked) ) ;
- break;
-
-
- case sdevGetDisplayWidth:
- result = csm->GetDisplayWidth();
- break;
-
- case sdevPeriodicTickle:
- result = csm->Periodic(statusRect, statusPort);
- break;
-
- case sdevDrawStatus:
- result = csm->Draw(statusRect, statusPort);
- break;
-
- case sdevMouseClick:
- result = csm->MouseDown(statusRect, statusPort);
- break;
-
- case sdevSaveSettings:
- result = csm->SaveSettings();
- break;
-
- case sdevShowBalloonHelp:
- result = csm->ShowBalloonHelp();
- break;
-
- }
-
- (void) SetA4(old_A4);
- return result;
- }